package com.zs.zcerp.salary.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zs.zcerp.entrance.entity.EpMain;
import com.zs.zcerp.entrance.entity.Syorg;
import com.zs.zcerp.entrance.service.IEpMainService;
import com.zs.zcerp.entrance.service.ISyorgService;
import com.zs.zcerp.salary.entity.*;
import com.zs.zcerp.salary.service.IEsDailySalaryService;
import com.zs.zcerp.salary.service.IMachineDeptsService;
import com.zs.zcerp.system.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api("件资接口")
@Controller
@RequestMapping("salary")
public class EsDailySalaryController {

    @Autowired
    private IEsDailySalaryService esDailySalaryService;

    @Autowired
    private IEpMainService epMainService;

    @Autowired
    private IMachineDeptsService machineDeptsService;

    @Autowired
    private ISyorgService syorgService;

    @ApiOperation("获取所有部门")
    @GetMapping("depts")
    @ResponseBody
    public String depts() {
        List<DeptDto> depts = esDailySalaryService.deptLists(null);
        Response response = new Response();
        response.setCode(200);
        response.setErrMsg("success");
        response.setData(depts);
        return JSON.toJSONString(response);
    }

    @ApiOperation("获取所有组织")
    @GetMapping("orgs")
    @ResponseBody
    public String orgs() {
        List<DeptDto> depts = esDailySalaryService.orgList();
        Response response = new Response();
        response.setCode(200);
        response.setErrMsg("success");
        response.setData(depts);
        return JSON.toJSONString(response);
    }

    @ApiOperation("获取选中的一个组织多个部门下面所有员工的最近一天件资数据")
    @ApiImplicitParams({})
    @GetMapping("dept/salary")
    @ResponseBody
    public String deptSalarys(@RequestBody JSONObject req) {
        JSONArray deptIds = req.getJSONArray("deptIds");
        List<EsDailySalary> esDailySalaries = esDailySalaryService.deptSalaryListSelected(deptIds);
        Response response = new Response();
        response.setCode(200);
        response.setErrMsg("success");
        response.setData(esDailySalaries);
        return JSON.toJSONString(response);
    }

    @ApiOperation("所有件资数据")
    @GetMapping("depts/salary")
    @ResponseBody
    public String salarys() {
        List<EsDailySalary> esDailySalaries = esDailySalaryService.deptSalaryListLatest();
        Response response = new Response();
        response.setCode(200);
        response.setErrMsg("success");
        response.setData(esDailySalaries);
        return JSON.toJSONString(response);
    }

    @ApiOperation("获取选中的多个组织多个部门下面所有员工的最近一天件资数据")
    @PostMapping("depts/salary")
    @ResponseBody
    public String deptSalary(@RequestBody JSONObject req) {
        JSONArray deptIds = req.getJSONArray("deptIds");
        List<EsDailySalary> esDailySalaries = esDailySalaryService.deptSalaryListLatestSelected(deptIds);
        Response response = new Response();
        response.setCode(200);
        response.setErrMsg("success");
        response.setData(esDailySalaries);
        return JSON.toJSONString(response);
    }

    @ApiOperation("获取预定分组的部门下面所有员工的最近一天件资数据")
    @GetMapping("depts/salary/group")
    @ResponseBody
    public String deptIdSalary(@RequestParam Integer groupId) {
        List<EsDailySalary> esDailySalaries = esDailySalaryService.deptGroupSalaryList(groupId);
        Response response = new Response();
        response.setCode(200);
        response.setErrMsg("success");
        response.setData(esDailySalaries);
        return JSON.toJSONString(response);
    }

    @ApiOperation("按机器编码获取预定分组的部门下面所有员工的最近一天件资数据")
    @GetMapping("machine/{machine}")
    @ResponseBody
    public String machineGetSalary(@PathVariable Integer machine) {
        // 查询机器编码保存的组别信息
        MachineDepts machineDepts = machineDeptsService.machineDepts(machine);
        if (machineDepts != null) {
            JSONArray depts = JSON.parseArray(machineDepts.getDepts());
            // 查询本组织的所有部门组别
            Integer orgId = Integer.valueOf(String.valueOf(machine).substring(0, 4));
            List<DeptDto> deptDtos = esDailySalaryService.deptList(orgId);
            // 查询组别的件资数据
            List<EsDailySalary> esDailySalaries = esDailySalaryService.deptSalaryList(orgId, depts);
            // 转换orgId中文名称
            Syorg syorg = syorgService.getSyorg(orgId);
            ResponseDto response = new ResponseDto();
            response.setCode(200);
            response.setErrMsg("success");
            response.setData(esDailySalaries);
            response.setDepts(deptDtos);
            response.setSelectedDepts(depts);
            response.setOrgName(syorg.getShortnmT());
            return JSON.toJSONString(response);
        } else {
            ResponseDto response = new ResponseDto();
            response.setCode(0);
            response.setErrMsg("No data!");
            return JSON.toJSONString(response);
        }

    }

    @ApiOperation("按机器编码获取预定分组的部门下面所有员工的最近一天件资数据, 并保存组别")
    @PostMapping("machine/{machine}")
    @ResponseBody
    public String machinePostSalary(@PathVariable Integer machine, @RequestBody JSONObject req) {
        machineDeptsService.machineDepts(machine);
        // 查询机器编码保存的组别信息
        MachineDepts machineDepts = machineDeptsService.machineDepts(machine);
        JSONArray depts = null;
        try {
            depts = req.getJSONArray("depts");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (depts != null && !depts.isEmpty()) {
            // 保存组别信息
            machineDeptsService.saveDepts(machine, depts);
        } else {
            try {
                depts = JSON.parseArray(machineDepts.getDepts());
            } catch (Exception e) {
            }
        }

        // 查询本组织的所有部门组别
        Integer orgId = Integer.valueOf(String.valueOf(machine).substring(0, 4));
        List<DeptDto> deptDtos = esDailySalaryService.deptList(orgId);

        ResponseDto response = new ResponseDto();
        if ((depts == null || depts.isEmpty()) && (machineDepts == null || machineDepts.getDepts().isEmpty()) && deptDtos.isEmpty()) {
            response.setCode(0);
            response.setErrMsg("No data!");
        } else {
            if (depts != null && !depts.isEmpty()) {
                // 查询组别的件资数据
                List<EsDailySalary> esDailySalaries = esDailySalaryService.deptSalaryList(orgId, depts);
                response.setData(esDailySalaries);
                response.setSelectedDepts(depts);
            }
            try {
                // 转换orgId中文名称
                Syorg syorg = syorgService.getSyorg(orgId);
                response.setOrgName(syorg.getShortnmT());
                response.setCode(200);
                response.setErrMsg("success");
                response.setDepts(deptDtos);
            } catch (Exception e) {
                response.setCode(0);
                response.setErrMsg("error, orgId error");
            }
        }
        return JSON.toJSONString(response);
    }

    @ApiOperation("获取个人一段时间内的件资数据")
    @PostMapping("personal")
    @ResponseBody
    public String personalSalary(@RequestBody JSONObject req) {
        String empNo = req.getString("empNo").toUpperCase();
        Date startTime = null;
        Date endTime = null;
        try {
            startTime = req.getDate("startTime");
            endTime = req.getDate("endTime");
        } catch (Exception e) {
            e.printStackTrace();
        }
        EpMain epMain = epMainService.findByEmpno(empNo);
        Response response = new Response();
        if (epMain == null) {
            response.setCode(0);
            response.setErrMsg("没有查到该员工记录, 请重新输入");
            return JSON.toJSONString(response);
        }
        List<EsDailySalary> personal = esDailySalaryService.findByEmpNo(epMain.getEmpCode(), startTime, endTime);
        if (personal.isEmpty()) {
            response.setCode(0);
            response.setErrMsg("没有查到该员工记录, 请重新输入");
            return JSON.toJSONString(response);
        } else {
            response.setCode(200);
            response.setErrMsg("success");
            response.setData(personal);
            JSONObject jsonObject = (JSONObject) JSON.toJSON(response);
            jsonObject.put("sex", "F".equals(epMain.getGender()) ? "女" : "男");
            jsonObject.put("inDate", epMain.getHiredDate());
            return JSON.toJSONString(jsonObject);
        }
    }

    @ApiOperation("手机端获取个人一段时间内的件资数据")
    @PostMapping("personal/mobile")
    @ResponseBody
    public String personalistSalary(@RequestBody JSONObject req) {
        User user =(User) SecurityUtils.getSubject().getPrincipal();
        String empNo = user.getUsername();
        Date startTime = null;
        Date endTime = null;
        try {
            startTime = req.getDate("startTime");
            endTime = req.getDate("endTime");
        } catch (Exception e) {
            e.printStackTrace();
        }

        EpMain epMain = epMainService.findByEmpno(empNo);
        Response response = new Response();
        if (epMain == null) {
            response.setCode(0);
            response.setErrMsg("没有查到该员工记录, 请重新输入");
            return JSON.toJSONString(response);
        }
        List<EsDailySalary> personal = esDailySalaryService.findByEmpNo(epMain.getEmpCode(), startTime, endTime);
        if (personal.isEmpty()) {
            response.setCode(0);
            response.setErrMsg("没有查到该员工记录, 请重新输入");
            return JSON.toJSONString(response);
        } else {
            response.setCode(200);
            response.setErrMsg("success");
            response.setData(personal);
            JSONObject jsonObject = (JSONObject) JSON.toJSON(response);
            jsonObject.put("sex", "F".equals(epMain.getGender()) ? "女" : "男");
            jsonObject.put("inDate", epMain.getHiredDate());
            return JSON.toJSONString(jsonObject);
        }
    }

    @ApiOperation("手机端获取个人一段时间内的件资数据测试用接口")
    @PostMapping("personal/mobile/test")
    @ResponseBody
    public String personalistSalaryTest(@RequestBody(required = false) JSONObject req) {
        String empNo = "S1503250";
        Date startTime = null;
        Date endTime = null;
        try {
            startTime = req.getDate("startTime");
            endTime = req.getDate("endTime");
        } catch (Exception e) {
            e.printStackTrace();
        }

        EpMain epMain = epMainService.findByEmpno(empNo);
        Response response = new Response();
        if (epMain == null) {
            response.setCode(0);
            response.setErrMsg("没有查到该员工记录, 请重新输入");
            return JSON.toJSONString(response);
        }
        List<EsDailySalary> personal = esDailySalaryService.findByEmpNo(epMain.getEmpCode(), startTime, endTime);
        if (personal.isEmpty()) {
            response.setCode(0);
            response.setErrMsg("没有查到该员工记录, 请重新输入");
            return JSON.toJSONString(response);
        } else {
            response.setCode(200);
            response.setErrMsg("success");
            response.setData(personal);
            JSONObject jsonObject = (JSONObject) JSON.toJSON(response);
            jsonObject.put("sex", "F".equals(epMain.getGender()) ? "女" : "男");
            jsonObject.put("inDate", epMain.getHiredDate());
            return JSON.toJSONString(jsonObject);
        }
    }

}
